package com.morgankelly.action;

import java.util.ArrayList;
import java.util.List;

import com.morgankelly.po.Tag;
import com.morgankelly.util.Constants;

@SuppressWarnings("serial")
public class TagAction extends BaseAction {

	public String input() {

		initTagForm();
		return INPUT;
	}
	
	public String add() {

	    Tag tag = new Tag();
	    tag.setName(name);
	    tag.setNumInBlog(0); //When we start to add a new tag, it is 0 by default.
	    getBlogService().addTag(tag);
	    
	    initTagForm();
		return ADD;
	}
	
	public String updateInput() {
		
		Tag tag;
		
		List<Object> args = new ArrayList<Object>();
		if (tagID == null || tagID == 0) {
            args.add(Tag.class.getSimpleName());
            addActionError(getText(Constants.ERROR_ID_NOT_CHOSEN, args));
            return FAILED;

		} else {
			tag = getBlogService().getTagByID(tagID);
			setName(tag.getName());
			setNumInBlog(tag.getNumInBlog());
		}
		
		initTagForm();
		return UPDATE_INPUT;
	}
	
	public String update() {
		
	    getBlogService().updateTag(tagID, name);
	    
		initTagForm();
		return UPDATE;
	}
	
	public String delete() {

		getBlogService().deleteTagByID(tagID);
		
		initTagForm();
		return DELETE;
	}
	
	
	private void initTagForm() {

		setTagList(getBlogService().getTagList());

		setNumInBlog(0);
		initSideBar();
	}

	private String name;
    private int numInBlog;
    private Long tagID;
    
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumInBlog() {
		return numInBlog;
	}

	public void setNumInBlog(int numInBlog) {
		this.numInBlog = numInBlog;
	}

	public Long getTagID() {
		return tagID;
	}

	public void setTagID(Long tagID) {
		this.tagID = tagID;
	}
    
}
